﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using timetableasy.Models;

namespace timetableasy.Controllers
{
    public class CampusFormViewModel 
    {
        // Properties
        public string CampusName{ get; private set; }
        public string Country { get; private set; }
        public string City {get; private set;}
    
        // Constructor
        public CampusFormViewModel(Campus campus) 
        {
            CampusName = campus.CampusName;
            Country = campus.Country;
            City = campus.City;
        }
    }
    public class CampusesController : Controller
    {
        CampusRepository campusRepository = new CampusRepository();
        //
        // GET: /Campuses/

        public ActionResult Index()
        {
            var campuses = campusRepository.FindAllCampuses().ToList();
            return View("Index", campuses);
        }

        //
        // GET: /Campuses/Details/5

        public ActionResult Details(int id)
        {
            Campus campus = campusRepository.GetCampusById(id);
            if (campus == null)
                return View("NotFound");
            else
                return View("Details", campus);
        }

        //
        // GET: /Campuses/Create

        public ActionResult Create()
        {
            Campus campus = new Campus()
            {
                CampusName = string.Empty,
                City = string.Empty,
                Country = string.Empty
            };

            return View(campus);
        } 

        //
        // POST: /Campuses/Create

        [AcceptVerbs(HttpVerbs.Post), Authorize]
        public ActionResult Create(Campus campus)
        {
            if (ModelState.IsValid)
            {

                try
                {
                    campusRepository.Add(campus);
                    campusRepository.Save();

                    return RedirectToAction("Details", new { id = campus.CampusId });
                }
                catch
                {
                    foreach (var issue in campus.GetRuleViolations())
                    {
                        ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                    }
                }
            }

            return View(campus);
        }

        //
        // GET: /Campuses/Edit/5
        
        public ActionResult Edit(int id)
        {
            Campus campus = campusRepository.GetCampusById(id);
            if (campus != null)
            {
                return View(campus);
            }
            else
            {
                return RedirectToAction("Index");
            }
            
        }

        //
        // POST: /Campuses/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Campus campus = campusRepository.GetCampusById(id);
            if (campus == null)
                return View("NotFound");
            try
            {
                UpdateModel(campus);
                campusRepository.Save();
                return RedirectToAction("Details", new { id = campus.CampusId });
            }
            catch
            {
                foreach (var issue in campus.GetRuleViolations())
                {
                    ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                }
                return View(campus);
            }
        }

        //
        // GET: /Campuses/Delete/5

        public ActionResult Delete(int id)
        {
            Campus campus = campusRepository.GetCampusById(id);

            if (campus == null)
                return View("NotFound");

            //if (!campus.IsHostedBy(User.Identity.Name))
            //    return View("InvalidOwner");

            return View(campus);
        }

        // 
        // HTTP POST: /Dinners/Delete/1

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id, string confirmButton)
        {
            Campus campus = campusRepository.GetCampusById(id);

            if (campus == null)
                return View("NotFound");

            //if (!campus.IsHostedBy(User.Identity.Name))
            //    return View("InvalidOwner");

            campusRepository.Delete(campus);
            campusRepository.Save();

            return View("Deleted");
        }
    }
}
